package com.alibaba.org.controller;

import com.alibaba.basic.annotation.PreAuthorize;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.PageList;
import com.alibaba.org.domain.Department;
import com.alibaba.org.query.DepartmentQuery;
import com.alibaba.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController // 复合注解: ResponseBody + Controller
@RequestMapping("/department") // 匹配请求
@Api(value = "部门接口",description = "部门的CRUD功能") // Swagger注解：对Controller做描述
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口：查询单个对象
     * http://localhost:8080/department/1 + get
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    @PreAuthorize(name="查询单个对象",sn = "department:getOne")
    public Department findOne(@PathVariable("id") Long id) {
        return departmentService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     * http://localhost:8080/department + 无 + get
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有对象")
    @PreAuthorize(name="查询所有对象",sn = "department:getAll")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * 接口：分页查询
     * http://localhost:8080/department
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    @PreAuthorize(name="部门列表",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 接口：删除
     * http://localhost:8080/department/1 + delete
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize(name="删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * http://localhost:8080/department + {json对象}
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
    @PreAuthorize(name="添加或修改",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) { // 添加操作
                departmentService.add(department);
            } else { // 修改操作
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 接口：批量删除
     * http://localhost:8080/department/ + {json对象}
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    @PreAuthorize(name="批量删除",sn = "department:batchDelete")
    public AjaxResult batchDelete(@RequestBody Long[] ids) {
        try {
            departmentService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：无线级数
     * http://localhost:8080/department + 无 + get
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询上级部门")
    @PreAuthorize(name="部门树",sn = "department:deptTree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }

}
